<template>
  <div class="goodsTypeCreate">
    <el-card class="box-card">
      <div slot="header" class="clearfix header">
        <span>创建商品类型</span>
        <el-button type="primary" round @click="backFn">返回</el-button>
      </div>

      <el-form
        :model="ruleForm"
        status-icon
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="类型名称" prop="type_name">
          <el-input
            type="text"
            v-model="ruleForm.type_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { postGoodsTypeStateApi } from "@/api";
export default {
  data() {
    return {
      //表单添加数据
      ruleForm: {
        type_name: ""
      }
    };
  },

  methods: {
    //点击提交按钮时
    submitForm(refName) {
      //this.$refs[formName].validate((valid) => {}
      this.$refs[refName].validate(isSuccess => {
        if (isSuccess) {
          // this.$message.success('发送异步请求');
          postGoodsTypeStateApi(this.ruleForm).then(res => {
            if (res.meta.state == 201) {
              this.$message.success(res.meta.msg);
              //添加成功后跳转
              this.$router.push({ path: "/goods/type" });
            } else {
              this.$message.error(res.meta.msg);
            }
          });
        }
      });
    },
    resetForm(refName) {
      //  this.$refs[formName].resetFields();
      this.$refs[refName].resetFields();
    },

    backFn() {
      this.$router.push({ path: "/goods/type" });
    }
  }
};
</script>

<style lang="scss" scoped>
.goodsTypeCreate {
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
